#include <stdio.h>
#include <unistd.h>
#include "mpi.h"

int
main(int argc, char **argv)
{
	int size, rank, new_size;
	int a, b;
	MPI_Comm newcomm;
	MPI_Request request;
	MPI_Status status;
	
	MPI_Init(&argc, &argv);
	
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);

	MPI_Comm_dup(MPI_COMM_WORLD, &newcomm);

	MPI_Comm_size(newcomm,&new_size);

	if( new_size == size )
	{
		printf("SUCCESS!\n");
	}
	
	if (rank == 0)
	{
		a = 42;
		b = 43;
		MPI_Isend(&a, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &request);
		MPI_Send(&b, 1, MPI_INT, 1, 1, newcomm);
		printf("sent %d on MPI_COMM_WORLD\n", a);
		printf("sent %d on duplicate comm\n", b);
	}
	else
	{
		a = 0;
		b = 0;
		MPI_Recv(&b, 1, MPI_INT, 0, 1, newcomm, &status);
		MPI_Recv(&a, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
		printf("recvd %d on MPI_COMM_WORLD\n", a);
		printf("recvd %d on duplicate comm\n", b);
	}
		
	MPI_Finalize();
	return 0;
}

